package designPattern.singleton;

/**
 * 优化 加入synchronized 重量级锁
 */
public class singleton03 {

    private static singleton03 INSTANCE;

    private singleton03() {
    }

    public static synchronized singleton03 getInstance(){
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new singleton03();
        }
        return INSTANCE;
    }

    public void k(){
        System.out.println("OK");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread( ()-> System.out.println(singleton03.getInstance().hashCode())).start();
        }
    }
}
